package com.csu.config;

import cn.dev33.satoken.stp.StpInterface;
import com.csu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SaTokenConfig {
    @Autowired
    UserMapper userMapper;
    @Bean
    public StpInterface stpInterface(){
        return new StpInterface() {
            @Override
            public List<String> getPermissionList(Object o, String s) {
                List<String> list = new ArrayList<>();
                // 根据 loginId 获取用户的权限列表
                // ... 添加权限
                return list;
            }

            @Override
            public List<String> getRoleList(Object loginId, String s) {
                System.out.println("loginId" + loginId);
                List<String> list = new ArrayList<>();
                String strLoginId = loginId.toString();
                int intLoginId = Integer.parseInt(strLoginId);
                System.out.println("strLoginId" + strLoginId);
                int roleId = userMapper.getRoleIdByUserId(intLoginId);
                System.out.println("roleId:" + roleId);
                //根据不同的role_id返回对应的角色
                switch(roleId){
                    case 0:
                        list.add("admin");
                        list.add("user");
                        break;
                    case 1:
                        list.add("teacher");
                        list.add("user");
                        break;
                    case 2:
                        list.add("parent");
                        list.add("user");
                        break;
                    case 3:
                        list.add("student");
                        list.add("user");
                        break;
                    default:
                        list.add(null);
                }
                return list;
            }
        };
    }
}
